<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>添加模块</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="../../component/ui/css/ui.css"/>

    </head>
    <body>
        <form class="layui-form" action="" lay-filter="editform">
            <div class="mainBox">
                <div class="main-container">
                    <div class="main-container">
                        <div class="layui-form-item">
                            <label class="layui-form-label">模块名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入模块名称" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">模块标志</label>
                            <div class="layui-input-block">
                                <input type="text" name="key" lay-verify="required" autocomplete="off" placeholder="请输入模块标志" class="layui-input">
                                <div class="layui-form-mid layui-word-aux">不能和系统中的其他模块标志相同</div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">后台路径</label>
                            <div class="layui-input-block">
                                <input type="text" name="server_path" lay-verify="required" value="server" autocomplete="off" placeholder="请输入后台路径" class="layui-input">
                            </div>
                        </div>
                        
                        <div class="layui-form-item">
                            <label class="layui-form-label">模块类型</label>
                            <div class="layui-input-block">
                                <input type="radio" value="1" name="type" title="控制层" checked class="modck"  lay-filter="type">
                                <input type="radio" value="2" name="type" title="模型层" lay-filter="type">
                                <input type="radio" value="3" name="type" title="服务层" lay-filter="type">
                                <input type="radio" value="5" name="type" title="通用curd" lay-filter="type">
                            </div>
                        </div>

                         <div id="rule-area" style="display:none">
                            <div class="layui-form-item">
                                <label class="layui-form-label">上级权限</label>
                                <div class="layui-input-block">
                                <button class="layui-btn" id="chooseRule">
                                    <span id="ruleText">选择权限</span>
                                    <i class="layui-icon layui-icon-down layui-font-12"></i>
                                </button>
                                </div>
                            </div>
                             <div class="layui-form-item">
                                <label class="layui-form-label">图标</label>
                                <div class="layui-input-block">
                                <input type="text" name="icon" autocomplete="off" class="layui-input icon-selected"  value="layui-icon layui-icon-username"/>
                                <!--input name="icon" id="icon" style="display:none;"        value="layui-icon-face-smile-fine" 
                                class="layui-input" lay-filter="iconPicker"-->
                                </div>
                            </div>

                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">主表</label>
                            <div class="layui-input-block">
                                <select name="tables_main" lay-search id="key-area">
                                    <option value="">请选择表</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">附加表</label>
                            <div class="layui-input-inline" id="tables"></div>
                            <!--div class="layui-input-inline">
                                <input type="checkbox" name="iscate" lay-skin="switch" lay-text="分类|不分类">
                            </div>
                            <div class="layui-form-mid layui-word-aux">分类表应包含id/pid/title</div-->
                        </div>
                        
                        <div class="layui-form-item">
                            <label class="layui-form-label">全局常量</label>
                            <div class="layui-input-block" id="params"></div>
                        </div>

                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <textarea name="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
            <div class="bottom">
                <div class="button-container">
                    <input type="hidden" name="pid" value="-1" id="pid">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                        <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
                </div>
            </div>
        </form>
        <script src="../../config/config.js"></script>
        <script src="../../component/layui/layui.js"></script>
        <script src="../../component/ui/ui.js"></script>
        <script>
            layui.use([
                'form', 'layer', 'dropdown', 'iconSelectedPlus','selectM'
            ], function () {
                let form = layui.form,
                    layer = layui.layer,
                    $ = layui.$,
                    dropdown = layui.dropdown,
                    selectM = layui.selectM,
                    iconSelectedPlus = layui.iconSelectedPlus;
                form.on('radio(type)', function(data){
                    if(data.value == 5) {
                        $('#rule-area').show();
                    }else{
                        $('#rule-area').hide();
                    }
                });
                 iconSelectedPlus.render(".icon-selected", {
                    //position : 'right',
                    icons: iconSelectedPlus.icons.layui,
                    simple : true
                });
                /*
                iconPicker.render({
                    elem: "#icon",
                    type: "fontClass",
                    search: true,
                    page: true,
                    limit: 16,
                    cellWidth: '43px',
                    click: function (data) {},
                    success: function (d) {}
                });*/

                _get(layui, 'mod/addBefore', res => {
                    console.log(res)
                    //渲染表列表
                    res.tableList
                        .forEach(d => {
                            //console.log(d)
                            $("#key-area").append(new Option(d.name, d.id));
                        });
                    form.render("select");
                    //多选标签-所有配置
                    selectM({
                        //元素容器【必填】
                        elem: '#tables'
                        //候选数据【必填】
                        ,data: res.tableList
                        //最多选中个数，默认5
                        ,max : 100
                        //input的name 不设置与选择器相同(去#.)
                        ,name: 'tables_more'
                        //值的分隔符
                        ,delimiter: ','
                        //候选项数据的键名
                        ,field: {idName:'id',titleName:'name'}
                    });
                    selectM({
                        //元素容器【必填】
                        elem: '#params'
                        //候选数据【必填】
                        ,data: res.params
                        //最多选中个数，默认5
                        ,max : 100
                        //input的name 不设置与选择器相同(去#.)
                        ,name: 'params'
                        //值的分隔符
                        ,delimiter: ','
                        //候选项数据的键名
                        ,field: {idName:'id',titleName:'name'}
                    });
                    //console.log(moreBtn)
                    
                    //渲染权限
                    dropdown.render({
                        elem: '#chooseRule',
                        data: res.authTree,
                        trigger: 'hover',
                        click: (d, el) => {
                            console.log(d)
                            $('#ruleText').text(d.title);
                            $('#pid').val(d.id);
                        }
                    })
                    //监听提交
                    form.on('submit(user-save)', function (data) {
                        data = data.field;
                        if(data.tables_more && data.tables_main && data.tables_more.split(',').indexOf(data.tables_main) !== -1) {
                            layer.msg('附表中不能包含主表！');
                            return false;
                        }
                        //curd
                        if(data.type == 5 && !data.tables_main) {
                            layer.msg('curd必须选择主表！');
                            return false;
                        }
                        if(data.iscate && data.iscate == 'on'){
                            //console.log(data.tables_more)
                            if(!data.tables_more) {
                                layer.msg('请选择一个附加表！');
                                return false;
                            }
                            if(data.tables_more.indexOf(',') !== -1) {
                                layer.msg('附加表为分类时只能选择一个！');
                                return false;
                            }
                        }
                        //return false;
                        _post(layui, 'mod/add', data, rt => {
                            //console.log(res)
                            parent
                                .layui
                                .table
                                .reload("mod-table");
                            parent
                                .layer
                                .close(parent.layer.getFrameIndex(window.name)); //关闭当前页

                        })

                        return false;
                    });
                });
                
                

            });
        </script>

    </body>
</html>